﻿/****************************************************
	文件：ItemCell.cs
	作者：WithoutUmbrella_Boy
	邮箱: 1026188730@qq.com
	日期：2021/02/16 17:27   	
	功能：背包格子逻辑
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

/// <summary>
/// 格子类型枚举
/// </summary>
public enum E_Item_Type
{
    Bag = 0,
    Head,
    Neck,
    Weapon,
    Cloth,
    Trousers,
    Shoes,

}

/// <summary>
/// 道具格子对象
/// </summary>
public class ItemCell : BasePanel
{
    private ItemInfo _itemInfo;

    public E_Item_Type type = E_Item_Type.Bag;

    public Image imgBK;

    public Image imgIcon;

    private bool isOpenDrag = false;

    public ItemInfo itemInfo
    {
        get
        {
            return _itemInfo;
        }
    }

    protected override void Awake()
    {
        base.Awake();

        imgIcon = GetControl<Image>("imgIcon");
        imgBK = GetControl<Image>("imgBK");

        //一开始就把图标隐藏了
        imgIcon.gameObject.SetActive(false);


        //监听鼠标移入和鼠标移出事件来进行处理
        EventTrigger trigger = GetControl<Image>("imgBK").gameObject.AddComponent<EventTrigger>();

        //申明一个鼠标进入的事件类对象
        EventTrigger.Entry enter = new EventTrigger.Entry();
        enter.eventID = EventTriggerType.PointerEnter;
        enter.callback.AddListener(EnterItemCell);


        //申明一个鼠标退出的事件类对象
        EventTrigger.Entry exit = new EventTrigger.Entry();
        exit.eventID = EventTriggerType.PointerExit;
        exit.callback.AddListener(ExitItemCell);

        trigger.triggers.Add(enter);
        trigger.triggers.Add(exit);


    }

    /// <summary>
    /// 开启检测鼠标拖动相关事件
    /// </summary>
    private void OpenDragEvent()
    {
        if (isOpenDrag)
        {
            return;
        }
        isOpenDrag = true;
        EventTrigger trigger = GetControl<Image>("imgBK").GetComponent<EventTrigger>();

        //EventTrigger中的拖动相关知识点
        //开始拖动
        EventTrigger.Entry beginDrag = new EventTrigger.Entry();
        beginDrag.eventID = EventTriggerType.BeginDrag;
        beginDrag.callback.AddListener(BeginDragItemCell);


        //拖动中
        EventTrigger.Entry drag = new EventTrigger.Entry();
        drag.eventID = EventTriggerType.Drag;
        drag.callback.AddListener(DragItemCell);


        //结束拖动
        EventTrigger.Entry endDrag = new EventTrigger.Entry();
        endDrag.eventID = EventTriggerType.EndDrag;
        endDrag.callback.AddListener(EndDragItemCell);

        trigger.triggers.Add(beginDrag);
        trigger.triggers.Add(drag);
        trigger.triggers.Add(endDrag);
    }


    private void BeginDragItemCell(BaseEventData data)
    {
        Debug.Log("开始拖动");
        EventCenter.GetInstance().EventTrigger<ItemCell>("ItemCellBeginDrag", this);
    }
    private void DragItemCell(BaseEventData data)
    {
        Debug.Log("拖动中");
        EventCenter.GetInstance().EventTrigger<BaseEventData>("ItemCellDrag", data);
    }
    private void EndDragItemCell(BaseEventData data)
    {
        Debug.Log("结束拖动");
        EventCenter.GetInstance().EventTrigger<ItemCell>("ItemCellEndDrag", this);
    }



    private void EnterItemCell(BaseEventData data)
    {
        EventCenter.GetInstance().EventTrigger<ItemCell>("ItemCellEnter", this);
    }

    private void ExitItemCell(BaseEventData data)
    {

        EventCenter.GetInstance().EventTrigger<ItemCell>("ItemCellExit", this);

    }



    /// <summary>
    /// 根据道具信息初始化格子信息
    /// </summary>
    public void InitInfo(ItemInfo info)
    {
        this._itemInfo = info;
        //如果是空，隐藏目标即可
        if (info == null)
        {
            imgIcon.gameObject.SetActive(false);
            imgIcon.sprite = null;//修复bug1
            return;
        }
        
        //只要设置了信息应该激活图片
        imgIcon.gameObject.SetActive(true);
        //根据道具信息的数据来更新格子对象
        Item itemData = GameDataMgr.GetInstance().GetItemInfo(info.id);
        //使用我们道具表中的数据
        //图标
        //通过道具ID得到的道具表中的数据信息 就可以得到对应的道具ID用的图标是什么加载它来用即可
        imgIcon.sprite = ResMgr.GetInstance().Load<Sprite>("Icon/" + itemData.icon);
        //数量
        if (type == E_Item_Type.Bag)
        {
            GetControl<Text>("txtNum").text = info.num.ToString();
            if (itemData.type==2)
            {
                GetControl<Text>("txtNum").color = Color.clear;
            }
        }


        if (itemData.type == (int)E_Bag_Type.Equip)
        {
            OpenDragEvent();
        }
    }
}
